﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EQT.Dal;
using EQT.Model;
using Common.Logging;

public partial class FinancialInterface: System.Web.UI.Page
{
    private DaProductCheckContract daContract = DevFuture.Common.WorkContext.AppCtx.GetObject("DaProductCheckContract") as DaProductCheckContract;
    private static readonly ILog log = LogManager.GetCurrentClassLogger();
    private UserContext context;

    protected void Page_Load(object sender, EventArgs e)
    {
        context = Session["USER_CONTEXT"] as UserContext;
        AspNetPager1.PageSize = 10;
        AspNetPager2.PageSize = 10;
        divResult.Visible = false;
        OperationResult.Text = "";

        if (!Page.IsPostBack)
        {
            this.ContactQuery();
            this.ContactQueryPayed();
        }
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        this.ContactQuery();
    }

    protected void AspNetPager2_PageChanged(object sender, EventArgs e)
    {
        this.ContactQueryPayed();
    }

    readonly static string strSQL_ContactQuery = DaProductCheckContract.SQL_PCC_VIEW + ContractStatusCollection.NO_PAY_CONTRACTS_STATUS;

    private void ContactQuery()
    {
        int count = 0;
        //string strSQL = @"SELECT t.*,'未缴费' as ContactStatus FROM Contract t WHERE substring(cast(t.ReportStatus as char(4)),4,1) ='0' and len(cast(t.ReportStatus as char(4)))='4'";

         
        GridViewContract.DataSource = DaCommon.GetPaginationData(strSQL_ContactQuery,
            AspNetPager1.PageSize, 
            AspNetPager1.CurrentPageIndex - 1, "Report_ID DESC", out count);
        AspNetPager1.RecordCount = count;
        GridViewContract.DataBind();       
        if (count == 0)
        {
            divResult.Visible = true;
            OperationResult.Text = "尚无符合条件的合同记录！";
        }
        else
        {
            divResult.Visible = false;
            OperationResult.Text = "";
        }
    }

    readonly static string strSQL_ContactQueryPayed = DaProductCheckContract.SQL_PCC_VIEW + ContractStatusCollection.HAS_PAID_CONTRACTS_STATUS;

    private void ContactQueryPayed()
    {
        int count = 0;
        //string strSQL = @"SELECT t.*,'已缴费' as ContactStatus FROM Contract t WHERE substring(cast(t.ReportStatus as char(4)),4,1) ='1' and len(cast(t.ReportStatus as char(4)))='4'";

        GridViewContractPayed.DataSource = DaCommon.GetPaginationData(strSQL_ContactQueryPayed,
            AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex - 1, " Report_ID  DESC", out count);
        AspNetPager2.RecordCount = count;
        GridViewContractPayed.DataBind();
        
    }

    protected void btnPay_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string Report_ID = (string)GridViewContract.DataKeys[gvr.RowIndex].Value;
        
        //更新合同状态为已缴费。
        if (daContract.SetPayed(Report_ID))
        {
            this.ContactQuery();
            this.ContactQueryPayed();
            divResult.Visible = true;
            OperationResult.Text = "更新合同[" + Report_ID + "]状态成功！";
        }
        else
        {
            divResult.Visible = true;
            OperationResult.Text = "更新合同[" + Report_ID + "]状态失败！";
        }
    }
}
